package com.chixing.config;


import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author YHB
 * @date 2021/11/2  21:17
 */
@Configuration
public class RabbitMQConfig {
    /**
     * //创建一个订单交换机，可以发送延迟消息
     */
    @Bean
    CustomExchange orderPluginDirect() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange("ORDER_EXCHANGE", "x-delayed-message",true, false,args);    }
    /**
     * 订单延迟插件队列
     */
    @Bean
    public Queue orderPluginQueue() {
//        Map<String, Object> args = new HashMap<>();
//        args.put("x-dead-letter-exchange", "ORDER_EXCHANGE");
//        args.put("x-dead-letter-routing-key", "KEY");
//        //声明队列消息过期时间
//        args.put("x-message-ttl", 10000);
//        return new Queue("ORDER_QUEUE", true, false, false,args);
        return new Queue("ORDER_QUEUE", true,false,false);
    }

    /**
     * 将订单延迟插件队列绑定到订单交换机
     */
    @Bean
    public Binding orderPluginBinding() {
        return BindingBuilder
                .bind(orderPluginQueue())
                .to(orderPluginDirect())
                .with("ORDER.KEY")
                .noargs();
    }

//    //--------------优惠券超时-------------------------
    /**
     * //创建一个优惠券交换机，可以发送延迟消息
     */
    @Bean
    CustomExchange couponPluginDirect() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange("COUPON_EXCHANGE", "x-delayed-message",true, false,args);    }
//    return new Jackson2JsonMessageConverter(QueueEnum.QUEUE_ORDER_PLUGIN_CANCEL.getExchange(), "x-delayed-message",true, false,args);
    /**
     * 优惠券延迟插件队列
     */
    @Bean
    public Queue couponPluginQueue() {
//        Map<String, Object> args = new HashMap<>();
//        args.put("x-dead-letter-exchange", "COUPON_EXCHANGE");
//        args.put("x-dead-letter-routing-key", "COUPON.KEY");
//        //声明队列消息过期时间
//        args.put("x-message-ttl", 10000);
//        return new Queue("COUPON_QUEUE", true, false, false, args);
        return new Queue("COUPON_QUEUE", true,false,false);
    }
    /**
     * 将优惠券延迟插件队列绑定到优惠券交换机
     */
    @Bean
    public Binding couponPluginBinding() {
        return BindingBuilder
                .bind(couponPluginQueue())
                .to(couponPluginDirect())
                .with("COUPON.KEY")
                .noargs();
    }

}
